﻿using System;

namespace QDP.Platform.Silverlight.Manager
{
    /// <summary>表示组件事件的监听者</summary>
    public class EventMonitor
    {
        /// <summary>当一个应用被打开时触发。</summary>
        public event EventHandler<WidgetEventArgs> Showing;
        /// <summary>当一个应用被打开后触发。</summary>
        public event EventHandler<WidgetEventArgs> Showed;
        /// <summary>当一个应用被关闭时触发。</summary>
        public event EventHandler<WidgetEventArgs> Closeing;
        /// <summary>当一个应用被关闭后触发。</summary>
        public event EventHandler<WidgetEventArgs> Closed;
        /// <summary>当一个应用被最小化时触发。</summary>
        public event EventHandler<WidgetEventArgs> Minimizing;
        /// <summary>当一个应用被最小化后触发。</summary>
        public event EventHandler<WidgetEventArgs> Minimized;
        /// <summary>当一个应用被恢复时触发。</summary>
        public event EventHandler<WidgetEventArgs> Restoring;
        /// <summary>当一个应用被恢复后触发。</summary>
        public event EventHandler<WidgetEventArgs> Restored;

        internal bool ResumeRestoringEvent(object sender, string Code)
        {
            if (Restoring != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Restoring(sender, args);
                    return args.IsCancel;
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
            return false;
        }
        internal void ResumeResizedEvent(object sender, string Code)
        {
            if (Restored != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Restored(sender, args);
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
        }

        internal bool ResumeShowingEvent(object sender, string Code)
        {
            if (Showing != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Showing(sender, args);
                    return args.IsCancel;
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
            return false;
        }
        internal void ResumeShowedEvent(object sender, string Code)
        {
            if (Showed != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Showed(sender, args);
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
        }

        internal bool ResumeMinimizingEvent(object sender, string Code)
        {
            if (Minimizing != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Minimizing(sender, args);
                    return args.IsCancel;
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
            return false;
        }
        internal bool ResumeClosingEvent(object sender, string Code)
        {
            if (Closeing != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Closeing(sender, args);
                    return args.IsCancel;
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
            return false;
        }
        internal void ResumeClosedEvent(object sender, string Code)
        {
            if (Closed != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Closed(sender, args);
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
        }
        internal void ResumeMinimizedEvent(object sender, string Code)
        {
            if (Minimized != null && sender != null)
            {
                var args = new WidgetEventArgs() { Name = Code };
                try
                {
                    Minimized(sender, args);
                }
                catch (Exception ex)
                {
                    this.ShowMessage(ex.Message,ex.ToString());
                }
            }
        }
    }
}